import {SerializedObject} from './SerializedObject';
import {Preconditions} from '../Preconditions';
import {BackendFileRef} from '../datastore/Datastore';

export class Image extends SerializedObject implements IImage {

    public readonly id: string;

    public readonly type: ImageType;

    public readonly src: BackendFileRef;

    public readonly width?: number;

    public readonly height?: number;

    public readonly rel?: string;

    constructor(opts: IImage) {

        super(<any> opts);

        this.id = opts.id;
        this.type = opts.type;
        this.src = opts.src;
        this.width = opts.width;
        this.height = opts.height;
        this.rel = opts.rel;

        this.init(opts);

    }


    public validate(): void {

        super.validate();

        Preconditions.assertPresent(this.type, "type");
        Preconditions.assertPresent(this.src, "src");

    }

}

export interface IImage {

    /**
     * A unique ID for this image which is used to store in keys and uniquely
     * represents this image.
     */
    readonly id: string;

    /**
     * The type of this image.
     */
    readonly type: ImageType;

    /**
     * The src of this Image as backed in the datastore.
     */
    readonly src: BackendFileRef;

    /**
     * The width of this image.
     */
    readonly width?: number;

    /**
     * The height of this image.
     *
     * @type {number}
     */
    readonly height?: number;

    /**
     * A per image 'relation' similar to the HTML rel attribute with links.
     * This allow us to attach an image to an annotation and give it a relation.
     *
     * For example.  We could have 'screenshot', 'thumbnail', 'highlight', etc.
     *
     * These relations are free form so any relation type can be designed by
     * the developer and still compatible with the schema.  Standard relations
     * are and will be defined and future relations can be added at any point.
     */
    readonly rel?: string;

}

export interface ImageOpts {
    readonly width?: number;
    readonly height?: number;
    readonly rel?: string;
    readonly type?: ImageType;
}

export type ImageType = 'image/gif' | 'image/png' | 'image/jpeg' | 'image/webp' | 'image/svg+xml';

export enum ImageTypes {
    GIF = 'image/gif',
    PNG = 'image/png',
    JPEG = 'image/jpeg',
    WEBP = 'image/webp',
    SVG = 'image/svg+xml'
}

